package com.freen.fadmin.config;

import com.freen.fadmin.module.filter.JwtAuthorizationFilter;
import com.freen.fadmin.service.jwt.JwtSysuserSecurityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MultiHttpSecurityConfig {
    @Configuration
    @Order(1)
    public class WebSecurityConfigForJwtSysuser extends WebSecurityConfigurerAdapter{
        @Autowired
        private AuthenticationSuccessHandler jwtSysuserAuthenticationSuccessHandler;
        @Autowired
        private AuthenticationFailureHandler jwtSysuserAuthenticationFailureHandler;

        //装载 BCrypt 密码编码器
        @Bean
        public PasswordEncoder passwordEncoderJwtSysuser(){
            return new BCryptPasswordEncoder();
        }

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.antMatcher("/SysApi/**")
                    .formLogin()
                    .usernameParameter("username")
                    .passwordParameter("password")
                    .loginPage("/SysApi/login")
                    .successHandler(jwtSysuserAuthenticationSuccessHandler)
                    .failureHandler(jwtSysuserAuthenticationFailureHandler)
                .and()
                    .authorizeRequests()
                    .antMatchers(
                            "/SysApi/addSysUser"
                        ,"/SysApi/addSysRole"
                        ,"/SysApi/addSysPermission"
                    )
                    //注意 为了添加用户方便，下面是放开了所有权限，上生产的时候记得关闭或者调整权限。
                    .permitAll()
                .and()
                    .addFilter(new JwtAuthorizationFilter(authenticationManager()));

            http.logout().permitAll();
            http.cors().and().csrf().ignoringAntMatchers("/SysApi/**");
        }

        @Bean
        UserDetailsService JwtSysuserSecurityService(){
            return new JwtSysuserSecurityService();
        }

        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.userDetailsService(JwtSysuserSecurityService()).passwordEncoder(new BCryptPasswordEncoder(){});
        }
    }



























}
